<script setup>
import { ref,onMounted,getCurrentInstance } from 'vue';
import { useCartStore } from '@/stores/cart';
import ShoppingCart from '@/components/ShoppingCart.vue';
import PackageItem from '@/components/PackageItem.vue';
import { getUserIsBuyService } from '@/api/type.js'
const store = useCartStore();
const { $t } = getCurrentInstance().proxy;
const packages = ref([
  { id: 1, name: $t('package1'), description: $t('package1Desc'), price: 39, added: false, owned: false },
  { id: 2, name: $t('package2'), description: $t('package2Desc'), price: 19, added: false, owned: false },
  { id: 3, name: $t('package3'), description: $t('package3Desc'), price: 15, added: false, owned: false },
  { id: 4, name: $t('package4'), description: $t('package4Desc'), price: 29, added: false, owned: false },
  { id: 5, name: $t('package5'), description: $t('package5Desc'), price: 39, added: false, owned: false },
  { id: 6, name: $t('package6'), description: $t('package6Desc'), price: 99, added: false, owned: false },
  { id: 7, name: $t('package7'), description: $t('package7Desc'), price: 15, added: false, owned: false },
  { id: 8, name: $t('package8'), description: $t('package8Desc'), price: 199, added: false, owned: false },
]);
const showCart = ref(false);
// 发请求获取哪些礼包当前用户已拥有
const getOwnedItems = async () => {
  let res = await getUserIsBuyService();
  packages.value.forEach(pkg => {
    const key = `package${pkg.id}Owned`;
    pkg.owned = res.data.hasOwnProperty(key) ? res.data[key] : false;
    console.log(pkg.owned);
  });
};
getOwnedItems();
// const ownedItems = ref([{id: 3},{id: 7}]);
onMounted(() => {
  packages.value.forEach(pkg => {
    pkg.added = store.cartItems.some(item => item.id === pkg.id);
    // pkg.owned = ownedItems.value.some(item => item.id === pkg.id);
  });
});
const addToCart = (pkg) => {
  pkg.added = true;
  store.addToCart({
    id: pkg.id,
    price: pkg.price
  });
};

const removeFromCart = (id) => {
  store.removeFromCart(id);
  const pkg = packages.value.find(pkg => pkg.id === id);
  if(pkg) pkg.added = false;
};

const cartTotal = () => store.cartTotal()
</script>

<template>
  <div id="page2" class="tab-page">
    <div class="cart-button" @click="showCart = !showCart">
      <i class="fas fa-shopping-cart"></i>
      <span class="cart-count" v-if="store.cartItems.length > 0">{{ store.cartItems.length }}</span>
    </div>

    <ShoppingCart :showCart="showCart" :cartItems="store.cartItems" :cartTotal="cartTotal()" @close="showCart = false"
      @remove="removeFromCart" />

    <header>
      <div class="logo">
        <i class="fas fa-crown"></i> AI Image Pro
      </div>
      <h1>{{ $t("giftTitle") }}</h1>
      <p class="subtitle">
        {{ $t("giftSubtitle") }}
      </p>
    </header>

    <div class="grid-container">
      <PackageItem v-for="pkg in packages" :key="pkg.id" :pkg="pkg" @add-to-cart="addToCart" />
    </div>
  </div>
</template>

<style scoped>
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css");

.tab-page {
  position: relative;
  height: 100%;
  overflow-y: auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%);
  background-color: var(--card-bg);
}

header {
  text-align: center;
  padding: 20px;
  background-color: var(--wrap-bg);
}

.logo {
  font-size: 2.5rem;
  margin-bottom: 10px;
  color: #4facfe;
  font-weight: 700;
}

h1 {
  font-size: 2.2rem;
  margin-bottom: 10px;
  color: var(--header-color);
}

.subtitle {
  font-size: 1.1rem;
  color: var(--secondary-text);
  max-width: 750px;
  margin: 0 auto;
  line-height: 1.6;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 25px;
  padding-top: 30px;
  padding-bottom: 50px;
  background-color: var(--wrap-bg);
}

.cart-button {
  position: fixed;
  top: 20px;
  right: 80px;
  background: var(--card-bg);
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  z-index: 100;
  transition: all 0.3s ease;
}

.cart-button:hover {
  transform: scale(1.05);
  box-shadow: 0 7px 20px rgba(0, 0, 0, 0.15);
}

.cart-button i {
  font-size: 1.5rem;
  color: #4facfe;
}

.cart-count {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #ff6b6b;
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: bold;
}

@media (max-width: 1200px) {
  .grid-container {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 992px) {
  .grid-container {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .grid-container {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 1.8rem;
  }

  .logo {
    font-size: 2rem;
  }
}

@media (max-width: 480px) {
  .cart-button {
    top: 15px;
    right: 15px;
    width: 50px;
    height: 50px;
  }
}
</style>